//
//  MobilephoneCharge.m
//  EasyToo
//
//  Created by linshengqin on 13-8-31.
//
//

#import "MobilephoneChargeView.h"
#import "MobilephoneChargeConfirmViewController.h"
#import "MobileModel.h"
#import <QuartzCore/QuartzCore.h>
#import "PhoneCardInfo.h"
#import "DeviceUtil.h"

#define SCREEN_HEIGHT [DeviceUtil getScreenHeight]
@implementation MobilephoneChargeView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //[self setBackgroundColor:[UIColor whiteColor]];
        [self setBackgroundColor:BackgroundColor];
        mobiledata = [MobileModel sharedata];


    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [self initScrollView];
    [self initView];
}
-(void)initScrollView
{
    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight])];
    [self addSubview:_bgScrollView];
    _bgScrollView.contentSize = CGSizeMake(320, 700);
    _bgScrollView.showsVerticalScrollIndicator = YES;
    
}

-(void) initView
{
    UILabel *titleLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(30, 20.0f/480.0f*SCREEN_HEIGHT, 250, 30)];
    [titleLabel1 setText:@"填写手机信息"];
    [titleLabel1 setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:titleLabel1];
    UIImage *image = [UIImage imageNamed:@"select_button"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image] ;
    imageView.frame = CGRectMake(20, 30.0f/480.0f*SCREEN_HEIGHT, 10, 10);
    [_bgScrollView addSubview:imageView];

    
    self.phoneNum = [[UITextField alloc] initWithFrame:CGRectMake(30, 60.0f/480.0f*SCREEN_HEIGHT, 250, 30)];
    [self.phoneNum setBorderStyle:UITextBorderStyleBezel];
    self.phoneNum.borderStyle = UITextBorderStyleRoundedRect;
    self.phoneNum.layer.borderWidth = 1 ;
    self.phoneNum.layer.cornerRadius = 10 ;
    self.phoneNum.placeholder = @"请输入手机号码";
    self.phoneNum.delegate = self;
//    self.phoneNum.keyboardType = UIKeyboardTypeNumberPad;
    self.phoneNum.returnKeyType = UIReturnKeyDone;
    [_bgScrollView addSubview:self.phoneNum];
    self.phoneNum.delegate = self;
    self.phoneNum.tag = 1;
    
    // 建立 UIToolbar
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(cancelPicker)];
    // 把按鈕加進 UIToolbar
    toolBar.items = [NSArray arrayWithObject:right];
    //self.phoneNum.inputAccessoryView = toolBar;
    
    pickerArray = [[NSMutableArray arrayWithObjects:@"30",@"50",@"100",@"300", nil] retain];
    selectPicker = [[UIPickerView alloc]initWithFrame:CGRectZero];
    selectPicker.showsSelectionIndicator = YES;
    selectPicker.frame = CGRectMake(0, 100.0f/480.0f*SCREEN_HEIGHT, 0, 0);
    selectPicker.delegate = self;
    selectPicker.dataSource = self;
    
    //self.phoneNum.inputAccessoryView = toolBar;
    
    
    selectPicker.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    //self.phoneNum.inputView = selectPicker;
    
    UILabel *titleLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(30, 100.0f/480.0f*SCREEN_HEIGHT, 250, 30)];
    [titleLabel2 setText:@"请选择充值面额"];
    [titleLabel2 setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:titleLabel2];
    
    UIImageView *imageView2 = [[UIImageView alloc] initWithImage:image] ;
    imageView2.frame = CGRectMake(20, 110.0f/480.0f*SCREEN_HEIGHT, 10, 10);
    [_bgScrollView addSubview:imageView2];

    
    self.chargePrice = [[UITextField alloc] initWithFrame:CGRectMake(30, 140.0f/480.0f*SCREEN_HEIGHT, 250, 30)];
    [self.chargePrice setBorderStyle:UITextBorderStyleBezel];
    self.chargePrice.borderStyle = UITextBorderStyleRoundedRect;
    self.chargePrice.layer.borderWidth = 1 ;
    self.chargePrice.layer.cornerRadius = 10 ;
    [_bgScrollView addSubview:self.chargePrice];
    self.chargePrice.delegate = self;
    self.chargePrice.tag = 2;
    self.chargePrice.inputAccessoryView = toolBar;
    selectPicker.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    self.chargePrice.inputView = selectPicker;
    
    UILabel *titleLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(30, 170.0f/480.0f*SCREEN_HEIGHT, 250, 30)];
    [titleLabel3 setText:@"选择支付方式"];
    [titleLabel3 setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:titleLabel3];
    
    UIImageView *imageView3 = [[UIImageView alloc] initWithImage:image] ;
    imageView3.frame = CGRectMake(20, 180.0f/480.0f*SCREEN_HEIGHT, 10, 10);
    [_bgScrollView addSubview:imageView3];
    
//    self.zfbButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 210.0f/480.0f*SCREEN_HEIGHT, 100, 30)];
//    //[self.zfbButton setBackgroundImage:[UIImage imageNamed:@"zfb"] forState:UIControlStateNormal];
//    [self.zfbButton addTarget:self action:@selector(zfbButtonPick:) forControlEvents:UIControlEventTouchDown];
//    [self.zfbButton setBackgroundImage:[UIImage imageNamed:@"zfb.png"] forState:UIControlStateNormal];
//    [self.zfbButton setBackgroundImage:[UIImage imageNamed:@"zhifubao2.png"] forState:UIControlStateSelected];
//    [_bgScrollView addSubview:self.zfbButton];
    
    self.ylButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 210.0f/480.0f*SCREEN_HEIGHT, 100, 30)];
    //[ylButton setBackgroundImage:[UIImage imageNamed:@"yinlian1"] forState:UIControlStateNormal];
    [self.ylButton addTarget:self action:@selector(ylButtonPick:) forControlEvents:UIControlEventTouchDown];
    [self.ylButton setBackgroundImage:[UIImage imageNamed:@"yinlian1.png"] forState:UIControlStateNormal];
    [self.ylButton setBackgroundImage:[UIImage imageNamed:@"yinlian2.png"] forState:UIControlStateSelected];
    [_bgScrollView addSubview:self.ylButton];
    
    UIButton *nextStep =[UIButton buttonWithType:UIButtonTypeCustom];
    nextStep.frame = CGRectMake(40, 260.0f/480.0f*SCREEN_HEIGHT, 230, 40);
    [nextStep setBackgroundImage:[UIImage imageNamed:@"button_3"] forState:UIControlStateNormal];
    [nextStep setTitle:@"下一步" forState:UIControlStateNormal];
    [_bgScrollView addSubview:nextStep];
    [nextStep addTarget:self action:@selector(confirmMessge:) forControlEvents:UIControlEventTouchUpInside];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // When the user presses return, take focus away from the text field so that the keyboard is dismissed.
//    NSTimeInterval animationDuration = 0.30f;
//    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
//    [UIView setAnimationDuration:animationDuration];
//    CGRect rect = CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height);
//    self.frame = rect;
//    [UIView commitAnimations];
    [textField resignFirstResponder];
    return YES;
}

-(void)confirmMessge:(id)sender
{
    MobilephoneChargeConfirmViewController  *confirmView = [[[MobilephoneChargeConfirmViewController alloc] init]autorelease];
    [self.fatherView.navigationController pushViewController:confirmView animated:YES];
    
}


#pragma picker delegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [pickerArray count];
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [pickerArray objectAtIndex:row];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:UITextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
    }
    // Fill the label text here
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect frame = textField.frame;
    int offset = frame.origin.y + 32 - (self.frame.size.height - 216);//键盘高度216
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    float width = self.frame.size.width;
    float height = self.frame.size.height;
    if(offset > 0)
    {
        CGRect rect = CGRectMake(0.0f, -offset,width,height);
        self.frame = rect;
    }
    [UIView commitAnimations];
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    NSInteger row = [selectPicker selectedRowInComponent:0];
    if( textField.tag == 1)
    {
        [mobiledata.phoneNum setString:[textField text]];
        NSLog(@"%@",mobiledata.phoneNum);
        //get phoneNum and send checkRequest
        if(![DeviceUtil isMobileNumber:[textField text]])
        {
            UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请输入正确的手机号" delegate:self cancelButtonTitle:@"ok" otherButtonTitles : nil];
            [alertview show] ;
            [alertview release];
        }
        else
        {
            [self checkPhoneInfo];
        }
    }
    if( textField.tag == 2)
    {
        if( [pickerArray count] > 0)
        {
            [mobiledata.price setString:[pickerArray objectAtIndex:row]];
            self.chargePrice.text = [pickerArray objectAtIndex:row];
        }
        if( [mobiledata.cardIdArray count] > 0)
        {
            [mobiledata.cardId setString:[mobiledata.cardIdArray objectAtIndex:row]];
        }

    }
}

// 按下完成鈕後的 method
-(void) cancelPicker {
    [self.chargePrice endEditing:YES];
}

//支付方式
-(void)zfbButtonPick:(id)sender
{
    [self.zfbButton setSelected:YES];
    [self.ylButton setSelected:NO];
    mobiledata.payType = 1;
}



-(void)ylButtonPick:(id)sender
{
    [self.zfbButton setSelected:NO];
    [self.ylButton setSelected:YES];
    mobiledata.payType = 0;
}

-(void)checkPhoneInfo
{
    mobiledata =[MobileModel sharedata];
    [PhoneManager shareManager].delegate = self;
    [[PhoneManager shareManager] checkPhoneCard:self.phoneNum.text];

}

- (void)onCheckPhoneChargeInfoFinish:(BOOL)isSuccess
                            cardArea:(NSString*)area
                            cardType:(NSString*)type
                            cardInfo:(NSMutableArray*)info
{
    if(isSuccess)
    {
        int i = 0;
        mobiledata.area = area;
        mobiledata.cardType = type;
        [mobiledata.phoneNum setString:self.phoneNum.text];
        for(PhoneCardInfo *obj in info)
        {
            [pickerArray replaceObjectAtIndex:i++ withObject:obj.cardPrice];
            [mobiledata.cardIdArray addObject:obj.cardId];
            //[mobiledata.cardInfoArray addObject:obj.cardPrice];
        }
        [selectPicker reloadAllComponents];
    }
}

@end
